package com.x.control;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.x.service.transactional.PaddingService;

@Controller
@RequestMapping("/exsit")
public class ExsitPaddingController {
	@Autowired(required = true)
	PaddingService paddingService;
	String[] columns = { "id", "fc", "shipMethod", "province", "city",
			"district", "padding", "startDate", "endDate", "submit.reason",
			"submit.createtime" };

	@RequestMapping(value = "/list")
	@ResponseBody
	public Map<String, Object> list(HttpServletRequest request) {
		int startIndex = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String draw = request.getParameter("draw");
		List<String[]> odrerPart = new ArrayList<>();
		List<String[]> wherePart = new ArrayList<>();
		for (int i = 0; i < columns.length; i++) {
			String orderColumn = request.getParameter("order[" + i
					+ "][column]");
			if (orderColumn != null) {
				String orderDir = request.getParameter("order[" + i + "][dir]");
				odrerPart.add(new String[] {
						columns[Integer.parseInt(orderColumn)], orderDir });
			}
			String searchable = request.getParameter("columns[" + i
					+ "][searchable]");
			if (searchable.equals("true")) {
				String searchColumn = columns[i];
				String value = request.getParameter("columns[" + i
						+ "][search][value]");
				if (!value.isEmpty()) {
					wherePart.add(new String[] { searchColumn, value });
				}
			}
		}
		Map<String, Object> result = paddingService.listExsit(startIndex,
				length, wherePart, odrerPart);
		result.put("draw", draw);
		return result;
	}

	@RequestMapping(value = "/deploy")
	public void deploy(final HttpServletResponse response) {
		paddingService.deploy(response);

	}
}
